package com.yantu.controller.wx;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yantu.bean.Class;
import com.yantu.bean.ClassStudent;
import com.yantu.bean.User;
import com.yantu.mapper.ClassMapper;
import com.yantu.mapper.ClassStudentMapper;
import com.yantu.mapper.UserMapper;
import com.yantu.util.JWTUtil;
import com.yantu.util.RedisUtils;
import com.yantu.util.Result;
import com.yantu.view.ModifyUserView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Api("学生用户接口")
@RestController
@RequestMapping("/wx")
public class WxUserController extends WxUserFatherController {

    @Autowired
    UserMapper userMapper;

    @Autowired
    ClassStudentMapper classStudentMapper;

    @Autowired
    ClassMapper classMapper;

    @Autowired
    RedisUtils redisUtils;


    @Autowired
    JWTUtil jwtUtil;

    @GetMapping("/user")
    @ApiOperation("获取当前用户个人信息")
    public Result getUserInfo(HttpServletRequest request){
        //获取请求头token
        String token = request.getHeader("token");
        //根据
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("token",token);
        User user = userMapper.selectOne(query);
        if (user==null){
            return Result.fail("获取信息",null);
        }
        //把查询到的信息放在需要的视图里面并返回给前端
        Map<String,Object> map = new HashMap<>();
        map.put("id",user.getId());
        map.put("userName",user.getUserName());
        map.put("realName",user.getRealName());
        map.put("age",user.getAge());
        map.put("role",user.getRole());
        map.put("sex",user.getSex());
        map.put("birthDay",user.getBirthDay());
        map.put("phone",user.getPhone());
        map.put("createTime",user.getCreateTime());
        map.put("lastActiveTime",user.getLogintime());
        map.put("status",user.getStatus());
        map.put("userLevel",user.getUserLevel());
        map.put("imagePath",user.getImagePath());
        //查出班级id
        QueryWrapper<ClassStudent> query2 = new QueryWrapper<>();
        query2.eq("user_id",user.getId());
        List<ClassStudent> css = classStudentMapper.selectList(query2);
        System.out.println("aaaaaaaaaaaaenmezheyanga");
        System.out.println(css);
        //根据班级id查出班级名称
        if(css!=null) {
            int m=0;
            for (ClassStudent cs:css) {
                System.out.println(cs.getClassId());
                if(m==0)
                {
                    Class SClass = classMapper.selectById(cs.getClassId());
                    map.put("classes", SClass.getName());
                }
                m++;
            }
        }else {
            map.put("classes", null);
        }
        return Result.succ(200,"获取成功",map);
    }

    @PutMapping("/user")
    @ApiOperation("修改用户个人信息")
    public Result updateUserInfo(HttpServletRequest request,@RequestBody ModifyUserView modifyUserView){
        //获取请求头token
        String token = request.getHeader("token");
        //获取openid
//        String openid = jwtUtil.getOpenidFromToken(token);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("token",token);
        User user = userMapper.selectOne(queryWrapper);
        System.out.println(modifyUserView);

        System.out.println(user);
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",user.getId());
        if(modifyUserView.getAge()!=null){
            updateWrapper.set("age",modifyUserView.getAge());
        }
        if(modifyUserView.getPhone()!=null){
            updateWrapper.set("phone",modifyUserView.getPhone());
        }
        if(modifyUserView.getBirthDay()!=null){
            updateWrapper.set("birth_day",modifyUserView.getBirthDay());
        }
        if(modifyUserView.getRealName()!=null){
            updateWrapper.set("real_name",modifyUserView.getRealName());
        }
        if(modifyUserView.getSex()!=null){
            updateWrapper.set("sex",modifyUserView.getSex());
        }
        int result  = userMapper.update(null,updateWrapper);
        System.out.println(result);
        if (result > 0){
            //查出班级id
            System.out.println("更新成功了");
            System.out.println(result);
            QueryWrapper<ClassStudent> query2 = new QueryWrapper<>();
            query2.eq("user_id",user.getId());
            System.out.println(user);
            ClassStudent cs = classStudentMapper.selectOne(query2);
            System.out.println(cs);
            //根据班级id查出班级名称
            Class SClass = classMapper.selectById(cs.getClassId());
            //把查询到的信息放在需要的视图里面并返回给前端
            Map<String,Object> map = new HashMap<>();
            map.put("id",user.getId());
            map.put("userName",user.getUserName());
            map.put("realName",user.getRealName());
            map.put("age",user.getAge());
            map.put("role",user.getRole());
            map.put("sex",user.getSex());
            map.put("birthDay",user.getBirthDay());
            map.put("phone",user.getPhone());
            map.put("createTime",user.getCreateTime());
            map.put("lastActiveTime",user.getLogintime());
            map.put("status",user.getStatus());
            map.put("userLevel",user.getUserLevel());
            map.put("classes",SClass.getName());
            map.put("imagePath",user.getImagePath());
            String msg = "学生"+user.getUserName()+"修改信息成功";
            super.wxAddLog(user,msg);
            redisUtils.delAll();
            return Result.succ("修改成功",map);
        }else {
            String msg = "学生" + user.getUserName() + "修改信息失败";
            super.wxAddLog(user,msg);
            return Result.fail("修改失败");
        }
    }

    @ApiOperation("查询用户日志信息")
    @GetMapping("/userLog")
    public Result getUserLog(HttpServletRequest request) {
        String token = request.getHeader("token");
        if(redisUtils.haskeysf("wxUserLog"+token)){
            return (Result) redisUtils.getValue("wxUserLog"+token);
        }else {
        Result result = super.userInfoList(token);
        if(result.getData()!=null){
            redisUtils.saveValue("wxUserLog"+token,result);
            return result;
        }else if(result.getMsg()=="用户没有日志"){
            return Result.fail("用户没有日志");
        }else {
            return Result.fail("没有该用户");
        }
    }
    }
}
